package com.wjjun.library.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RadioGroup
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingListener
import androidx.databinding.InverseBindingMethod
import androidx.databinding.InverseBindingMethods

/**
 * 支持双向绑定的单选按钮组
 * author : wangjunjun
 * date   : 2022/7/26
 * desc   :
 */
@InverseBindingMethods(
    InverseBindingMethod(
        type = DBRadioGroup::class,
        attribute = "checkedValue",
        event = "checkedValueAttrChanged",
        method = "getCheckedValue"
    )
)
class DBRadioGroup : RadioGroup {

    private var checkedValue: Int? = 0
    private var listener: OnValueChangedListener? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {

    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        setOnCheckedChangeListener { _: RadioGroup, checkedId: Int ->
            if (checkedId > 0) {
                val radioButton =
                    findViewById<View>(checkedId) as DBRadioButton
                setCheckedValue(radioButton.value)
            } else {
                setCheckedValue(null)
            }
        }
    }


    fun isCheckedValue(value: Int): Boolean {
        if (checkedValue == null) {
            return false
        }
        return value == checkedValue
    }

    fun setCheckedValue(checked: Int?) {
        if (checked == null) {
            checkedValue = null
            clearCheck()
            return
        }
        if (checkedValue == checked) {
            return
        }

        this.checkedValue = checked
        val customRadioButton = findViewById<DBRadioButton>(
            checkedRadioButtonId
        )
        if (customRadioButton == null || checkedValue != customRadioButton.value) {
            for (i in 0 until childCount) {
                val child = getChildAt(i)
                if (child is DBRadioButton) {
                    if (isCheckedValue(child.value)) {
                        child.isChecked = true
                    }
                }
            }
        }
        listener?.onValueChanged()
    }

    fun getCheckedValue(): Int? {
        return checkedValue
    }

    fun setListener(listener: OnValueChangedListener) {
        this.listener = listener
    }

    interface OnValueChangedListener {
        fun onValueChanged()
    }

    companion object {
        @JvmStatic
        @BindingAdapter("checkedValueAttrChanged")
        fun setValueChangedListener(
            view: DBRadioGroup,
            bindingListener: InverseBindingListener
        ) {
            // 通知 ViewModel
            view.setListener(object : OnValueChangedListener {
                override fun onValueChanged() {
                    bindingListener.onChange()
                }
            })
        }
    }
}